using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NBear.Common;
using NBear.IoC.Service;

public partial class controls_Messages : NBear.Web.UI.AjaxTemplate
{
    protected int curPage;
    protected int maxPage;
    protected string info;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadMessagePage(1);
    }

    private void LoadMessagePage(int pageNo)
    {
        curPage = pageNo;
        if (Session["admin"] != null && (bool)Session["admin"] == true)
        {
            maxPage = ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().GetMessagePageCount(5, true);
            msgItems.DataSource = ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().GetMessages(5, pageNo, true);
        }
        else
        {
            maxPage = ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().GetMessagePageCount(5, false);
            msgItems.DataSource = ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().GetMessages(5, pageNo, false);
        }
        msgItems.DataBind();
    }

    public override void OnAjaxTemplatePreRender(System.Collections.Generic.Dictionary<string, string> callbackParams)
    {
        if (callbackParams["option"] == "add")
        {
            SimpleGuestbook.Entities.Message msg = EntityFactory<SimpleGuestbook.Entities.Message>.CreateObject();
            msg.Nick = callbackParams["nick"];
            msg.Email = callbackParams["email"];
            msg.Content = callbackParams["content"];
            msg.IsPrivate = (callbackParams["isPrivate"] == "true");
            msg.PostTime = DateTime.Now;
            msg.AdminFeedbackTime = DateTime.Now;

            ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().AddMessage(msg);

            LoadMessagePage(1);
        }
        else if (callbackParams["option"] == "page")
        {
            LoadMessagePage(ParsePage(callbackParams["pageNo"]));
        }
        else if (callbackParams["option"] == "login")
        {
            if (ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IAdminService>().VerifyPassword(callbackParams["password"]))
            {
                info = ClientScriptFactory.WrapScriptTag(ClientScriptFactory.PopAlert("Login OK!"));
                Session["admin"] = true;
            }
            else
            {
                info = ClientScriptFactory.WrapScriptTag(ClientScriptFactory.PopAlert("Admin password incorrect, please try again!"));
                Session["admin"] = false;
            }

            LoadMessagePage(ParsePage(callbackParams["pageNo"]));
        }
        else if (callbackParams["option"] == "logout")
        {
            Session["admin"] = false;
            LoadMessagePage(ParsePage(callbackParams["pageNo"]));
        }

        //admin option only
        if (Session["admin"] != null && (bool)Session["admin"] == true)
        {
            if (callbackParams["option"] == "delete")
            {
                ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().DeleteMessage(int.Parse(callbackParams["id"]));

                LoadMessagePage(ParsePage(callbackParams["pageNo"]));
            }
            else if (callbackParams["option"] == "feedback")
            {
                ServiceFactory.Create().GetService<SimpleGuestbook.ServiceInterfaces.IMessageService>().UpdateAdminFeedback(int.Parse(callbackParams["id"]), callbackParams["content"]);

                LoadMessagePage(ParsePage(callbackParams["pageNo"]));
            }
        }
    }

    private int ParsePage(string pageStr)
    {
        try
        {
            return int.Parse(pageStr);
        }
        catch
        {
        }

        return 1;
    }
}
